﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using Perfect.Utils.App;
using Perfect.Utils.Security2;
using Perfect.Utils.System2;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            string str;
            byte[] bytes;
            byte[] numArray;
            if (toDecrypt != "")
            {
                byte[] numArray1 = Convert.FromBase64String(toDecrypt);
                if (!useHashing)
                {
                    bytes = Encoding.UTF8.GetBytes(key);
                }
                else
                {
                    MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
                    bytes = mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(key));
                }
                TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider()
                {
                    Key = bytes,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
                ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateDecryptor();
                try
                {
                    numArray = cryptoTransform.TransformFinalBlock(numArray1, 0, (int)numArray1.Length);
                }
                catch
                {
                    throw new ArgumentException("File Data is bad.");
                }
                str = Encoding.UTF8.GetString(numArray);
            }
            else
            {
                str = "";
            }
            return str;
        }

        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            string base64String;
            byte[] bytes;
            if (toEncrypt != "")
            {
                byte[] numArray = Encoding.UTF8.GetBytes(toEncrypt);
                if (!useHashing)
                {
                    bytes = Encoding.UTF8.GetBytes(key);
                }
                else
                {
                    MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
                    bytes = mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(key));
                }
                TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider()
                {
                    Key = bytes,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
                ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
                byte[] numArray1 = cryptoTransform.TransformFinalBlock(numArray, 0, (int)numArray.Length);
                base64String = Convert.ToBase64String(numArray1, 0, (int)numArray1.Length);
            }
            else
            {
                base64String = "";
            }
            return base64String;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = "HPqrGX/piQz0Xe4u/h/67gQ3uyF/l8Jnvceflfn0g5a097J0Dujgoj8bqG7F7buoYB2Mt+NKyXJ+aWtOxGIikFoHuo//n+w+4LNbjrv8kNf5wfFV0DkffAhGApf8T2Dq";

           textBox2.Text =  Encrypt(textBox1.Text, "07071983", true);
        }

        public static string smethod_2()
        {
            //string software = SystemInfoHelper.GetSoftware("Perfect.HumanResource");
            //string systemInfo = SystemInfoHelper.GetSystemInfo(software);
            string systemInfo = "123";
            string str = "123";
            systemInfo = Md5Encrypt(systemInfo);
            str = string.Concat(str, systemInfo.Substring(0, 8));
            str = string.Concat(str, "-", systemInfo.Substring(9, 4));
            str = string.Concat(str, "-", systemInfo.Substring(14, 4));
            str = string.Concat(str, "-", systemInfo.Substring(19, 4));
            return string.Concat(str, "-", systemInfo.Substring(24));
        }

        public static string Md5Encrypt(string phrase)
        {
            UTF8Encoding uTF8Encoding = new UTF8Encoding();
            byte[] numArray = (new MD5CryptoServiceProvider()).ComputeHash(uTF8Encoding.GetBytes(phrase));
            return System.Text.Encoding.UTF8.GetString(numArray);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = Decrypt(textBox2.Text, "07071983", true);
        }

        //Server=perfect.com.vn;Initial catalog =perfectmain; User ID=pf_info;password =connect2.0.1.4
    }
}
